package me.zhengjie.modules.website.repository;

import me.zhengjie.modules.website.domain.VisitReservationTable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author system
 * @since 2024-10-23 22:20:17
 */
@Component
public class VisitReservationTableSpecifications {

    public static Specification<VisitReservationTable> queryList(VisitReservationTable visitReservationTable) {
        return (root, query, builder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (visitReservationTable.getReservationId() != null) {
                predicates.add(builder.like(root.get("reservationId" ).as(String.class), "%" + visitReservationTable.getReservationId() + "%" ));
            }
            if (visitReservationTable.getVisitDate() != null) {
                predicates.add(builder.equal(root.get("visitDate" ).as(Date.class), visitReservationTable.getVisitDate()));
            }
            if (visitReservationTable.getVisitTimeSlot() != null) {
                predicates.add(builder.like(root.get("visitTimeSlot" ).as(String.class), "%" + visitReservationTable.getVisitTimeSlot() + "%" ));
            }
            if (visitReservationTable.getReservedPeople() != null) {
                predicates.add(builder.equal(root.get("reservedPeople" ).as(Integer.class), visitReservationTable.getReservedPeople()));
            }
            if (visitReservationTable.getBookedPeople() != null) {
                predicates.add(builder.equal(root.get("bookedPeople" ).as(Integer.class), visitReservationTable.getBookedPeople()));
            }
            if (visitReservationTable.getRemainingSlots() != null) {
                predicates.add(builder.equal(root.get("remainingSlots" ).as(Integer.class), visitReservationTable.getRemainingSlots()));
            }
            if (visitReservationTable.getReservationStatus() != null) {
                predicates.add(builder.like(root.get("reservationStatus" ).as(String.class), "%" + visitReservationTable.getReservationStatus() + "%" ));
            }
            Predicate[] p = new Predicate[predicates.size()];
            return query.where(builder.and(predicates.toArray(p))).getRestriction();
        };
    }

}
